Optical distance sensor for robots

This article was first published in the May 1999 issue of The Robot Builder.

 by Jim Ubersetzig

Have you ever wanted to add sensors to your robot - but weren't sure how to do it ? In this article I will show you how to build a distance measuring sensor for your robot. The sensor could be used by the robot to avoid running into walls or obstacles.

The optical proximity sensor described here uses light to measure the distance to the obstacle. The distance is reported as a number.  A suggested
arrangement is shown in figure 1.

Note that three sensors are mounted on the robot. If the robot were traveling down a corridor, the sensors would report distance from the walls, and
also report distance to anything the robot is about to run into.

The distance is accurately reported from 4 inches to three feet. Distances beyond three feet are reported as three feet.  The sensor is highly tolerant of room lighting (except sunlight) and is not affected by colors or reflectances of target objects.

Figure 2 shows two screw holes which are provided for mounting the sensor to the robot.

Sources

When the author purchased his Sharp GP2D02 sensors in Feb 98, there were few sources for the device. You could purchase the minimum order
from a Sharp distributor ( thousand units minimum buy ) or you could search for someone who has a few left over. The author purchased his sensors for $75 each from someone on the internet.
Today the situation has changed. You can buy your GP2D02 sensors from HVW company for about $40. They include the electrical connector.
HVW is at www.hvwtech.com/robotics.htm#dirrs Tel: (403) 730-8603, Fax: (403) 730-8903, Info@HVWTech.com

To use the software provided in this article, you will also need a Basic Stamp 2 computer ( around $50 ).

Note: To load the software in this article into the Basic Stamp, you need a PC. The stamp also uses the PC to display test results. If you use the
remaining Basic Stamp pins to build a robot base to mount the sensor on, then the robot could operate without the PC.

Skills Required

You will need certain minimum skills to complete the device described in this article:

- Soldering, electronic grade.
- Cutting plastic sheet.
- Safety skills to avoid injuries.
- Drilling holes.

Theory of Operation

The Sharp distance sensor uses an infra red light-emitting-diode (IR LED) as a light source.  The reflected light is brought through a lens which
focuses the light into a tiny spot which strikes a position-sensitive detector (PSD). The distance is determined by a principle called optical triangulation. This is shown in the figure 3. As the distance to the sensed object changes, the spot of light moves on the position-sensitive
detector, and a different distance  is reported.

Wiring the Sensor

Wiring the sensor is easy, because HVW company includes the connector with your purchase. When making the electrical connections, you should
include two resistors. If you leave them out, the sensor reading will be erratic. Connect the wires as per figure 4.

Software
' -----[ Title ]-----------------------------------------------------------
'
' File...... GP2D02.BS2
' Purpose... Distance Sensor
' Author.... Jim Ubersetzig
'            juberset@lmco.com
' Written... 24 Sept 98
' -----[ General Description ]---------------------------------------------
'
' The Sharp GP2D02 distance measuring sensor measures distance using
' optical triangulation.
'
' This software is for using the sensor with a Basic Stamp 2 computer.
' -----[ Detailed Description ]--------------------------------------------
'
' The distance is reported as a number in the range 225 to 70, which
' corresponds to distances from 4 inches to 3 feet. Distances greater
' than 3 feet are reported as three feet.
' -----[ BS2-IC Wiring Connections ]----------------------------------------
'
' pin 23 Ground | pin 1
' pin 21 +5VDC out | pin 3
' pin 20 IO15/Clock Out | pin 2   Distance sensor
' pin 19 IO14/Data In | pin 4   type gp2d02
'
' Note: The sensor performs best if two 10K resistors are used. Connect one
' 10K resistor from pin 20 to pin 2. The other 10K resistor goes from
' pin 2 to ground.
' -----[ Constants ]-------------------------------------------------------
'
PinClock CON 15
PinData CON 14
' -----[ Variables ]-------------------------------------------------------
'
alpha VAR word ' Sensor reading, approx 75 for 31 inches,
' 135 for 8 inches.
' -----[ Main Code ]-------------------------------------------------------
INPUT PinData ' Initialize the electronics.
HIGH PinClock
PAUSE 500
MainLoop:
GOSUB   ReadTheSensor
pause 1000 ' Display sensor information for one
second.
GOTO MainLoop ' Then read the sensor again.
ReadTheSensor:
LOW PinClock ' Start process by driving the clock low for 70 mS.
PAUSE 70
SHIFTIN PinData,PinClock,msbpost,[alpha] ' Then shift in the data,
HIGH PinClock ' and set the
clock high.
PAUSE 2
debug dec ? alpha ' Display sensor reading on a pc.
RETURN
' -----[ End of Software ]-------------------------------------------------------


Get It Working ?

On your PC type stamp2 <enter> and the stamp development system will come up. If you don't have this file ( stamp2.exe  approx 15K bytes ), it's a free download from the parallax web site. It also comes with the developer's kit, along with the cable. If you don't have the cable, instructions for building one are on the web site.

enough !

On your PC you should see a blue screen. Type in the software, then ALT-S  to save the software in a file. Type in a suitable file name, then <enter>.
Now ALT-Q to exit.  The software is saved on your PC. To operate the sonar sensor: On the PC type: stamp <enter>

You'll see the blue screen again.  ALT-L will let you select the software you have previously saved. Once you see the software on the PC display,
hook up the cable from your PC's com port to the stamp. Now ALT-R will start the software running. You should see a list of numbers on the computer screen. These are the distance numbers reported by the sensor. Place an object where the sensor should see it and the numbers should change. Experiment with different objects and different distances. The distance numbers reported should be used with the chart in figure 5.

The Future ?

The author encourages experimentation. After building the project, try changing something. You might find an improvement !

 

 

by Jim Ubersetzig